package com.bjpowernode.core;

/**
 * Created whit IntelliJ IDEA.
 * Description：
 * User：ZHONGCHEN
 * Date:2022-10-28
 * Time:15:54
 */

import com.bjpowernode.constant.Constant;
import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.concurrent.atomic.LongAdder;

/**
 * 展示下载信息
 */
public class DownLoadInfo implements Runnable {
    //下载文件的总大小
    private long httpFileContentLength;

    //本地已下载文件的大小
    private static LongAdder finishedSize = new LongAdder();

    //前一次下载的大小
    public volatile double prevSize;
    //本次累计下载的大小
    public static volatile LongAdder downSize = new LongAdder();

    public DownLoadInfo(long httpFileContentLength) {
        this.httpFileContentLength = httpFileContentLength;
    }

    @Override
    public void run() {
        //计算文件总大小 单位:mb
        String httpFileSize = String.format("%.2f", httpFileContentLength / Constant.MB);
        //计算每秒下载速度-kb
        int speed = (int) ((downSize.doubleValue() - prevSize) / 1024d);
        prevSize = downSize.doubleValue();

        //剩余文件的大小
        double remainSize = httpFileContentLength - finishedSize.doubleValue() - downSize.doubleValue();
        //计算剩余时间
        String remainTime = String.format("%.1f", remainSize / 1024d / speed);

        if ("Infinity".equalsIgnoreCase(remainTime)) {
            remainTime = "-";
        }
        //已下载大小
        String currentFileSize = String.format("%.2f", (downSize.doubleValue() - finishedSize.doubleValue()) / Constant.MB);

        String downInfo = String.format("已下载:%smb/%smb,速度:%skb/s,剩余时间: %ss",
                currentFileSize, httpFileSize, speed, remainTime);
        System.out.print("\r");
        System.out.print(downInfo);
    }
}
